import re
from functools import wraps

from flask import Blueprint, session, g, request
from flask.json import jsonify

from app import csrf
from app.app_1_0.auth.models import User

auth_bp = Blueprint('auth', __name__)

from .auth import login_require
from .views import role_list


@auth_bp.before_request
@csrf.exempt
def before_request():
    pass


def init_permission_v1(current_user):
    """
    用户权限初始化
    :return:
    """
    # 1 获取用户的所有角色
    # current_user = User()
    roles = current_user.roles.all()
    # 2 获取该角色下所有的权限
    perms_obj_list, perms = [], []
    for item in roles:
        perms_obj_list.extend(item.permissions)
    perms_obj_set = set(perms_obj_list)
    for item in perms_obj_set:
        perms.append(item.url)
    # 3 把权限写入到session中
    session['perms'] = perms


def init_permission(current_user):
    perms = current_user.user_perms()
    session['perms'] = perms


def permission_required(needed_perms):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # 判断权限......
            user_id = g.user_id
            user = User.query.filter_by(id=user_id).first()
            if not user.has_per(needed_perms):
                return jsonify({'code': 403, 'info': '无访问权限'})
            return func(*args, **kwargs)

        return wrapper
    return decorator


"""
def init_permissionxxx(request, user):
    ""
    权限和菜单信息初始化，以后使用时，需要在登陆成功后调用该方法将权限和菜单信息放入session
    :param request:
    :param user:
    :return:
    ""

    # 3. 获取用户信息和权限信息写入session
    permission_queryset = user.roles.filter(permissions__url__isnull=False).values('permissions__id',
                                                                                   'permissions__url',
                                                                                   'permissions__title',
                                                                                   'permissions__name',
                                                                                   'permissions__parent_id',
                                                                                   'permissions__parent__name',
                                                                                   'permissions__menu_id',
                                                                                   'permissions__menu__title',
                                                                                   'permissions__menu__icon',
                                                                                   ).distinct()

    menu_dict = {}  # 菜单字典,它是能成为菜单的权限,用于做菜单显示
    permission_dict = {}  # 权限列表,所有权限,用于做权限校验

    for row in permission_queryset:
        # 以url别名为key
        permission_dict[row['permissions__name']] = {
            # 权限id
            'id': row['permissions__id'],
            # url
            'url': row['permissions__url'],
            'title': row['permissions__title'],
            # 权限父id
            'pid': row['permissions__parent_id'],
            # 父id的name
            'pname': row['permissions__parent__name'],
        }

        # 获取菜单id
        menu_id = row.get('permissions__menu_id')
        # 如果菜单id为空,跳过此次循环
        if not menu_id:
            continue

        # 判断菜单id不在字典里面时,避免一级菜单重复
        if menu_id not in menu_dict:
            # 以菜单id为key
            menu_dict[menu_id] = {
                # value部分就是title,用来展示一级菜单
                'title': row['permissions__menu__title'],
                # 一级菜单的图标
                'icon': row['permissions__menu__icon'],
                # 二级菜单
                'children': [
                    # 二级菜单标题和url。注意:一级标题是不能点击的,所以它没有url
                    # 二级菜单是可以点击的,但是它没有图标
                    {'id': row['permissions__id'], 'title': row['permissions__title'], 'url': row['permissions__url']}
                ]
            }
        else:
            # 如果一级菜单还有二级菜单,就继续添加
            menu_dict[menu_id]['children'].append({'id': row['permissions__id'], 'title': row['permissions__title'], 'url': row['permissions__url']})

    request.session[settings.PERMISSION_SESSION_KEY] = permission_dict
    request.session[settings.MENU_SESSION_KEY] = menu_dict
    
    """